<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<html>
<head>
    <title>${document.title} - 知识库</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: #f5f5f5;
            color: #1a1a1a;
            line-height: 1.6;
        }
        
        .detail-container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 24px;
        }
        
        .detail-header {
            background: white;
            border-radius: 12px;
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
            padding: 32px;
            margin-bottom: 24px;
            border: 1px solid #e5e5e5;
        }
        
        .header-top {
            display: flex;
            align-items: center;
            justify-content: space-between;
            margin-bottom: 20px;
        }
        
        .back-btn {
            color: #666;
            text-decoration: none;
            font-size: 14px;
            display: flex;
            align-items: center;
            gap: 6px;
            transition: color 0.3s ease;
        }
        
        .back-btn:hover {
            color: #1a1a1a;
        }
        
        .document-title {
            font-size: 32px;
            font-weight: 700;
            color: #1a1a1a;
            margin-bottom: 16px;
            line-height: 1.2;
        }
        
        .document-meta {
            display: flex;
            align-items: center;
            gap: 24px;
            color: #666;
            font-size: 14px;
            margin-bottom: 24px;
            flex-wrap: wrap;
        }
        
        .meta-item {
            display: flex;
            align-items: center;
            gap: 6px;
        }
        
        .visibility-badge {
            padding: 4px 12px;
            border-radius: 20px;
            font-size: 12px;
            font-weight: 600;
            text-transform: uppercase;
        }
        
        .visibility-public {
            background: #e8f5e8;
            color: #2e7d32;
        }
        
        .visibility-private {
            background: #ffebee;
            color: #c62828;
        }
        
        .action-buttons {
            display: flex;
            gap: 12px;
            flex-wrap: wrap;
        }
        
        .btn {
            padding: 10px 20px;
            border-radius: 8px;
            text-decoration: none;
            font-weight: 500;
            transition: all 0.3s ease;
            border: none;
            cursor: pointer;
            font-size: 14px;
            display: inline-flex;
            align-items: center;
            gap: 6px;
        }
        
        .btn-primary {
            background: #1a1a1a;
            color: white;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
        }
        
        .btn-primary:hover {
            background: #333;
            transform: translateY(-1px);
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
        }
        
        .btn-secondary {
            background: white;
            color: #1a1a1a;
            border: 2px solid #e5e5e5;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        }
        
        .btn-secondary:hover {
            transform: translateY(-1px);
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15);
            border-color: #333;
        }
        
        .btn-danger {
            background: #d32f2f;
            color: white;
            box-shadow: 0 2px 8px rgba(211, 47, 47, 0.3);
        }
        
        .btn-danger:hover {
            background: #b71c1c;
            transform: translateY(-1px);
            box-shadow: 0 4px 12px rgba(211, 47, 47, 0.4);
        }
        
        .btn.favorited {
            background: #ffd700;
            color: #1a1a1a;
        }
        
        .detail-content {
            display: grid;
            grid-template-columns: 1fr 350px;
            gap: 24px;
        }
        
        .content-main {
            background: white;
            border-radius: 12px;
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
            padding: 32px;
            border: 1px solid #e5e5e5;
        }
        
        .content-text {
            font-size: 16px;
            line-height: 1.8;
            color: #1a1a1a;
            white-space: pre-wrap;
            word-wrap: break-word;
        }
        
        .content-sidebar {
            display: flex;
            flex-direction: column;
            gap: 24px;
        }
        
        .sidebar-card {
            background: white;
            border-radius: 12px;
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
            padding: 24px;
            border: 1px solid #e5e5e5;
        }
        
        .sidebar-title {
            font-size: 18px;
            font-weight: 600;
            color: #1a1a1a;
            margin-bottom: 16px;
            display: flex;
            align-items: center;
            gap: 8px;
        }
        
        .comment-form {
            margin-bottom: 24px;
        }
        
        .comment-textarea {
            width: 100%;
            min-height: 100px;
            padding: 12px;
            border: 2px solid #e5e5e5;
            border-radius: 8px;
            font-size: 14px;
            font-family: inherit;
            resize: vertical;
            transition: all 0.3s ease;
        }
        
        .comment-textarea:focus {
            outline: none;
            border-color: #333;
            box-shadow: 0 0 0 3px rgba(0, 0, 0, 0.1);
        }
        
        .comment-submit {
            background: #1a1a1a;
            color: white;
            border: none;
            padding: 8px 16px;
            border-radius: 6px;
            font-size: 14px;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.3s ease;
            margin-top: 8px;
        }
        
        .comment-submit:hover {
            background: #333;
            transform: translateY(-1px);
        }
        
        .comment-list {
            max-height: 400px;
            overflow-y: auto;
        }
        
        .comment-item {
            padding: 16px 0;
            border-bottom: 1px solid #f0f0f0;
        }
        
        .comment-item:last-child {
            border-bottom: none;
        }
        
        .comment-header {
            display: flex;
            align-items: center;
            justify-content: space-between;
            margin-bottom: 8px;
        }
        
        .comment-author {
            font-weight: 600;
            color: #1a1a1a;
            font-size: 14px;
        }
        
        .comment-time {
            color: #666;
            font-size: 12px;
        }
        
        .comment-content {
            color: #1a1a1a;
            font-size: 14px;
            line-height: 1.5;
            margin-bottom: 8px;
        }
        
        .comment-actions {
            display: flex;
            gap: 12px;
        }
        
        .comment-action {
            color: #666;
            text-decoration: none;
            font-size: 12px;
            transition: color 0.3s ease;
        }
        
        .comment-action:hover {
            color: #1a1a1a;
        }
        
        .comment-reply {
            margin-left: 20px;
            margin-top: 12px;
            padding-left: 16px;
            border-left: 2px solid #e5e5e5;
        }
        
        .info-item {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 8px 0;
            border-bottom: 1px solid #f0f0f0;
        }
        
        .info-item:last-child {
            border-bottom: none;
        }
        
        .info-label {
            color: #666;
            font-size: 14px;
        }
        
        .info-value {
            color: #1a1a1a;
            font-weight: 500;
            font-size: 14px;
        }
        
        .error-message {
            background: #ffebee;
            color: #c62828;
            padding: 12px 16px;
            border-radius: 8px;
            margin-bottom: 20px;
            border-left: 4px solid #f44336;
            font-size: 14px;
        }
        
        .success-message {
            background: #e8f5e8;
            color: #2e7d32;
            padding: 12px 16px;
            border-radius: 8px;
            margin-bottom: 20px;
            border-left: 4px solid #4caf50;
            font-size: 14px;
        }
        
        .empty-state {
            text-align: center;
            padding: 40px 20px;
            color: #666;
        }
        
        .empty-state-icon {
            font-size: 48px;
            margin-bottom: 16px;
            opacity: 0.5;
        }
        
        .empty-state-text {
            font-size: 16px;
            margin-bottom: 8px;
        }
        
        .empty-state-subtext {
            font-size: 14px;
            opacity: 0.7;
        }
        
        /* 响应式设计 */
        @media (max-width: 768px) {
            .detail-content {
                grid-template-columns: 1fr;
            }
            
            .header-top {
                flex-direction: column;
                align-items: flex-start;
                gap: 16px;
            }
            
            .action-buttons {
                width: 100%;
                justify-content: flex-start;
            }
            
            .document-meta {
                flex-direction: column;
                align-items: flex-start;
                gap: 12px;
            }
        }
        
        /* 滚动条美化 */
        .comment-list::-webkit-scrollbar {
            width: 6px;
        }
        
        .comment-list::-webkit-scrollbar-track {
            background: #f1f1f1;
            border-radius: 3px;
        }
        
        .comment-list::-webkit-scrollbar-thumb {
            background: #c1c1c1;
            border-radius: 3px;
        }
        
        .comment-list::-webkit-scrollbar-thumb:hover {
            background: #a8a8a8;
        }
    </style>
</head>
<body>
    <div class="detail-container">
        <div class="detail-header">
            <div class="header-top">
                <a href="/user/docs" class="back-btn">
                    <span>←</span>
                    返回文档列表
                </a>
                
                <div class="action-buttons">
                    <c:if test="${canEdit}">
                        <a href="/document/edit/${document.id}" class="btn btn-primary">
                            ✏️ 编辑
                        </a>
                    </c:if>
                    
                    <form action="/document/favorite/${document.id}" method="post" style="display:inline;">
                        <button type="submit" class="btn ${isFavorited ? 'favorited' : 'btn-secondary'}">
                            ${isFavorited ? '⭐' : '☆'} ${isFavorited ? '已收藏' : '收藏'}
                        </button>
                    </form>
                    
                    <a href="/document/export/${document.id}" class="btn btn-secondary">
                        📄 导出
                    </a>
                    
                    <c:if test="${canDelete}">
                        <form action="/document/delete/${document.id}" method="post" style="display:inline;">
                            <button type="submit" class="btn btn-danger" onclick="return confirm('确定要删除这个文档吗？此操作不可恢复。')">
                                🗑️ 删除
                            </button>
                        </form>
                    </c:if>
                </div>
            </div>
            
            <h1 class="document-title">${document.title}</h1>
            
            <div class="document-meta">
                <div class="meta-item">
                    <span>👤</span>
                    <span>作者: ${document.authorName}</span>
                </div>
                
                <c:if test="${not empty document.spaceName}">
                    <div class="meta-item">
                        <span>🏠</span>
                        <span>空间: ${document.spaceName}</span>
                    </div>
                </c:if>
                
                <div class="meta-item">
                    <span>📅</span>
                    <span>创建时间: ${fn:substring(document.createdAt, 0, 19)}</span>
                </div>
                
                <div class="meta-item">
                    <span>🔄</span>
                    <span>更新时间: ${fn:substring(document.updatedAt, 0, 19)}</span>
                </div>
                
                <!-- 文档可见性徽章 -->
                <c:choose>
                  <c:when test="${document.status == 'PUBLIC'}">
                    <span class="visibility-badge visibility-public">公开</span>
                  </c:when>
                  <c:when test="${document.status == 'PENDING'}">
                    <span class="visibility-badge visibility-pending">待审核</span>
                  </c:when>
                  <c:otherwise>
                    <span class="visibility-badge visibility-private">私有</span>
                  </c:otherwise>
                </c:choose>
            </div>
        </div>
        
        <div class="detail-content">
            <div class="content-main">
                <div class="content-text">${document.content}</div>
            </div>
            
            <div class="content-sidebar">
                <!-- 评论区域 -->
                <div class="sidebar-card">
                    <div class="sidebar-title">
                        💬 评论 (${fn:length(comments)})
                    </div>
                    
                    <c:if test="${canComment}">
                        <div class="comment-form">
                            <form action="/document/comment/create" method="post">
                                <input type="hidden" name="documentId" value="${document.id}">
                                <textarea class="comment-textarea" 
                                          name="content"
                                          placeholder="写下您的评论..." required></textarea>
                                <button type="submit" class="comment-submit">
                                    发表评论
                                </button>
                            </form>
                        </div>
                    </c:if>
                    
                    <div class="comment-list" id="commentList">
                        <c:choose>
                            <c:when test="${empty comments}">
                                <div class="empty-state">
                                    <div class="empty-state-icon">💬</div>
                                    <div class="empty-state-text">暂无评论</div>
                                    <div class="empty-state-subtext">成为第一个评论的人吧！</div>
                                </div>
                            </c:when>
                            <c:otherwise>
                                <c:forEach var="comment" items="${comments}">
                                    <div class="comment-item" data-comment-id="${comment.id}">
                                        <div class="comment-header">
                                            <div class="comment-author">${comment.authorName}</div>
                                            <div class="comment-time">${fn:substring(comment.createdAt, 0, 19)}</div>
                                        </div>
                                        <div class="comment-content">${comment.content}</div>
                                        <div class="comment-actions">
                                            <a href="#" class="comment-action" onclick="showReplyForm(${comment.id})">回复</a>
                                            <c:if test="${comment.authorId == user.id || canManageComments}">
                                                <form action="/document/comment/delete/${comment.id}" method="post" style="display:inline;">
                                                    <button type="submit" class="comment-action" style="background:none;border:none;cursor:pointer;" onclick="return confirm('确定要删除这条评论吗？')">删除</button>
                                                </form>
                                            </c:if>
                                        </div>
                                        
                                        <!-- 回复区域 -->
                                        <c:forEach var="reply" items="${comment.replies}">
                                            <div class="comment-reply">
                                                <div class="comment-header">
                                                    <div class="comment-author">${reply.authorName}</div>
                                                    <div class="comment-time">${fn:substring(reply.createdAt, 0, 19)}</div>
                                                </div>
                                                <div class="comment-content">${reply.content}</div>
                                                <div class="comment-actions">
                                                    <c:if test="${reply.authorId == user.id || canManageComments}">
                                                        <form action="/document/comment/delete/${reply.id}" method="post" style="display:inline;">
                                                            <button type="submit" class="comment-action" style="background:none;border:none;cursor:pointer;" onclick="return confirm('确定要删除这条回复吗？')">删除</button>
                                                        </form>
                                                    </c:if>
                                                </div>
                                            </div>
                                        </c:forEach>
                                    </div>
                                </c:forEach>
                            </c:otherwise>
                        </c:choose>
                    </div>
                </div>
                
                <!-- 文档信息 -->
                <div class="sidebar-card">
                    <div class="sidebar-title">
                        📋 文档信息
                    </div>
                    
                    <div class="info-item">
                        <span class="info-label">文档ID</span>
                        <span class="info-value">${document.id}</span>
                    </div>
                    
                    <div class="info-item">
                        <span class="info-label">字符数</span>
                        <span class="info-value">${fn:length(document.content)}</span>
                    </div>
                    
                    <div class="info-item">
                        <span class="info-label">字数</span>
                        <span class="info-value">${fn:length(fn:trim(document.content)) > 0 ? fn:length(fn:split(fn:trim(document.content), ' ')) : 0}</span>
                    </div>
                    
                    <c:if test="${document.lockedBy != null}">
                        <div class="info-item">
                            <span class="info-label">锁定状态</span>
                            <span class="info-value">🔒 被 ${document.lockedByUsername} 锁定</span>
                        </div>
                    </c:if>
                </div>
            </div>
        </div>
    </div>
    
    <script>
        // 显示回复表单
        function showReplyForm(commentId) {
            const content = prompt('请输入回复内容:');
            if (content && content.trim()) {
                // 创建隐藏表单并提交
                const form = document.createElement('form');
                form.method = 'POST';
                form.action = '/document/comment/create';
                
                const documentIdInput = document.createElement('input');
                documentIdInput.type = 'hidden';
                documentIdInput.name = 'documentId';
                documentIdInput.value = '${document.id}';
                
                const contentInput = document.createElement('input');
                contentInput.type = 'hidden';
                contentInput.name = 'content';
                contentInput.value = content.trim();
                
                const parentIdInput = document.createElement('input');
                parentIdInput.type = 'hidden';
                parentIdInput.name = 'parentId';
                parentIdInput.value = commentId;
                
                form.appendChild(documentIdInput);
                form.appendChild(contentInput);
                form.appendChild(parentIdInput);
                
                document.body.appendChild(form);
                form.submit();
            }
        }
    </script>
</body>
</html> 